<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app;

use app\exception\ApiBase;
use app\exception\Success;
use app\library\AppCode;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\Lang as FaLang;
use think\facade\Log;
use think\Response;
use Throwable;

/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{
    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        ValidateException::class,
    ];

    /**
     * 记录异常信息（包括日志或者其它方式记录）
     *
     * @access public
     * @param  Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {
        // 使用内置的方式记录异常日志
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        // Default Data
        $data['code'] = $e->getCode();
        $data['msg'] = $e->getMessage();
        $data['data'] = [];

        // Success Need Data
        if($e instanceof Success){
            $data['data'] = $e->getData();
        }
        // Env
        $isEnv = $this->app->env->get('APP_DEBUG',false);

        // Get API Error Code
        if($e instanceof ApiBase) {
            $data['code'] = $e->getCode();
        }else if($e instanceof Throwable || $e instanceof \Exception){

            $data['code'] = AppCode::SYSTEM_ERROR;
            $data['msg'] = 'system error';

            $errorLog['code'] = $data['code'];
            $errorLog['msg'] = $e->getMessage();
            // 记录错误日志
            $isEnv == true ? Log::channel('sql')->info($errorLog) : Log::channel('file')->info($errorLog) ;
        }

//        var_dump($e->getMessage());
        // 其他错误交给系统处理
//         return parent::render($request, $e);

        if(!$isEnv) {
            // 其他错误交给系统处理
            return parent::render($request, $e);
        }else{

            $data['msg'] = FaLang::get($data['msg']);

            return Response::create($data,'json');
        }

    }
}
